package com.zky.org.service.impl;

import com.zky.basic.service.impl.BaseServiceImpl;
import com.zky.org.domain.Department;
import com.zky.org.domain.Employee;
import com.zky.org.mapper.DepartmentMapper;
import com.zky.org.mapper.EmployeeMapper;
import com.zky.org.query.EmployeeQuery;
import com.zky.org.service.EmployeeService;
import com.zky.user.domain.Logininfo;
import com.zky.user.mapper.LogininfoMapper;
import com.zky.util.Md5Utils;
import com.zky.util.PageList;
import com.zky.util.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService  {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public List<Department> getDeptTree() {
        List<Department> all = departmentMapper.findAll();
        return all;
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.list(shopId);
    }

    @Transactional
    @Override
    public void add(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);//获取到自增ID

        //添加user的数据
        employee.setLogininfo_id(logininfo.getId());

        super.add(employee);

        employeeMapper.addByRole(employee);

    }
    @Transactional
    @Override
    public void delete(Long id) {
        //先查询登陆信息id
        Employee employee = employeeMapper.findOne(id);
        //删除logininfo
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除employee
        super.delete(id);
    }
    @Transactional
    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldEmployee = employeeMapper.findOne(employee.getId());//loginfo_id

        //修改user信息
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        super.update(employee);
        employeeMapper.updateRole(employee);

        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.findOne(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);

        }
    }
}
